package com.woniuxy.lrz.carlproject.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsPropertyAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsPropertyFindForm;
import com.woniuxy.lrz.carlproject.dao.GoodPropertyValueMapper;
import com.woniuxy.lrz.carlproject.model.GoodProperty;
import com.woniuxy.lrz.carlproject.dao.GoodPropertyMapper;
import com.woniuxy.lrz.carlproject.model.GoodPropertyValue;
import com.woniuxy.lrz.carlproject.service.GoodPropertyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.GoodsPropertyDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * <p>
 * 属性名表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
public class GoodPropertyServiceImpl extends ServiceImpl<GoodPropertyMapper, GoodProperty> implements GoodPropertyService {

    @Resource
    private GoodPropertyMapper goodPropertyMapper;
    @Resource
    private GoodPropertyValueMapper goodPropertyValueMapper;
    @Override
    public boolean addGoodsProperty(GoodsPropertyAddOrUpdateForm goodsPropertyAddOrUpdateForms) {
            GoodProperty goodProperty = BeanUtil.copyProperties(goodsPropertyAddOrUpdateForms, GoodProperty.class);
            GoodProperty property_name = goodPropertyMapper.selectOne(new QueryWrapper<GoodProperty>().eq("property_name", goodsPropertyAddOrUpdateForms.getPropertyName()));
            //验证新增还是删除
            if(goodsPropertyAddOrUpdateForms.getPropertyId()==null){
                //验证新增内容是否重复
                if(property_name!=null){
                    throw new AppException("属性名重复，请稍后再试！");
                }
                //新增
                goodPropertyMapper.insert(goodProperty);
            }
            //验证修改后的内容是否冲突
            if(property_name!=null&&goodProperty.getPropertyId()!=property_name.getPropertyId()){
                throw new AppException("属性名重复，请稍后再试！");
            }
            //修改
            goodPropertyMapper.updateById(goodProperty);

        return true;
    }

    @Override
    public boolean deleteGoodsProperty(Integer propertyId) {
        //验证是否是重复删除
        GoodProperty goodProperty = goodPropertyMapper.selectById(propertyId);
        if(goodProperty==null){
            throw new AppException("请勿重复操作");
        }
        goodPropertyMapper.deleteById(propertyId);
        goodPropertyValueMapper.delete(new QueryWrapper<GoodPropertyValue>().eq("property_id",propertyId));
        return true;
    }

    @Override
    public Page<GoodsPropertyDto> findGoodsProperty(GoodsPropertyFindForm goodsPropertyFindForm) {
        Page<GoodProperty> page=new Page<>(goodsPropertyFindForm.getPageNum(),goodsPropertyFindForm.getPageSize());
        Page<GoodsPropertyDto> goodsProperty = goodPropertyMapper.findGoodsProperty(page, goodsPropertyFindForm);
        return goodsProperty;
    }
}
